package com.ziyun.erp.web.modules.store.storeGoodsCategory.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.utility.New;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.goodsCategory.entity.GoodsCategoryEntity;
import com.ziyun.erp.modules.goodsCategory.service.GoodsCategoryService;
import com.ziyun.erp.modules.store.storeGoodsCategory.entity.StoreGoodsCategoryEntity;
import com.ziyun.erp.modules.store.storeGoodsCategory.service.StoreGoodsCategoryService;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 门店商品分类表
 *
 * @author xueqz
 * @email
 * @date 2018-09-25 10:32:35
 */
@RestController
@RequestMapping("storeGoodsCategory/storegoodscategory")
public class StoreGoodsCategoryController {

    @Autowired
    private StoreGoodsCategoryService storeGoodsCategoryService;

    @Autowired
    private GoodsCategoryService goodsCategoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("storeGoodsCategory:storegoodscategory:list")
    public List<StoreGoodsCategoryEntity> list(@RequestParam Map<String, Object> params){
//        return storeGoodsCategoryService.selectAllList();
        return storeGoodsCategoryService.selectList(new EntityWrapper<StoreGoodsCategoryEntity>().eq("del", 0));
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{categoryId}")
    @RequiresPermissions("storeGoodsCategory:storegoodscategory:info")
    public R info(@PathVariable("categoryId") Integer categoryId){
        StoreGoodsCategoryEntity storeGoodsCategory = storeGoodsCategoryService.selectById(categoryId);

        return R.ok().put("storeGoodsCategory", storeGoodsCategory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("storeGoodsCategory:storegoodscategory:save")
    public R save(@RequestBody StoreGoodsCategoryEntity storeGoodsCategory){
        storeGoodsCategory.setCreateName(UserUtils.getCurentUser().getUsername());
        storeGoodsCategoryService.insert(storeGoodsCategory);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("storeGoodsCategory:storegoodscategory:update")
    public R update(@RequestBody StoreGoodsCategoryEntity storeGoodsCategory){
        ValidatorUtils.validateEntity(storeGoodsCategory);
        storeGoodsCategoryService.updateAllColumnById(storeGoodsCategory);//全部更新

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("storeGoodsCategory:storegoodscategory:delete")
    public R delete(@RequestBody Integer categoryIds){
//        storeGoodsCategoryService.deleteBatchIds(Arrays.asList(categoryIds));
        storeGoodsCategoryService.flagDeleteGoodsCategory(categoryIds);
        return R.ok();
    }

    /**
     * 选择菜单(添加、修改菜单)
     */
    @RequestMapping("/select")
    public R select() {
        List<StoreGoodsCategoryEntity> storeGoodsCategoryList = storeGoodsCategoryService.select();
        return R.ok().put("storeGoodsCategoryList",storeGoodsCategoryList);
    }

    /**
     * 信息
     */
    @RequestMapping("/getStoreCategoryByZYCatId/{catId}")
    public R getStoreCategoryByZYCatId(@PathVariable("catId") Integer catId){
        Map<String, Object> map = New.hashMap();
        map.put("ziyun_category_id",catId);
        map.put("del",0);
        List<StoreGoodsCategoryEntity> storeGoodsCategoryEntityList = storeGoodsCategoryService.selectByMap(map);
        //获得商品类型
        GoodsCategoryEntity categoryEntity = goodsCategoryService.selectById(catId);
        if(storeGoodsCategoryEntityList.size() != 0){
            return R.ok().put("storeGoodsCategory", storeGoodsCategoryEntityList.get(0)).put("categoryEntity",categoryEntity);
        }else{
            return R.ok().put("storeGoodsCategory", null).put("categoryEntity",categoryEntity);
        }
    }

}
